﻿
@{
    Layout = null;
}
@model GN.Pay.Application.Dto.Output.ServiceMessageResultOutput<GN.Pay.Application.GatewayApp.Dto.Output.CashiePayApplyOutput>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link href="~/Scripts/layer/skin/default/layer.css" rel="stylesheet" />
    <link href="~/Content/Wap/resetWap.css" rel="stylesheet" />
    <link href="~/Content/Wap/pay.css" rel="stylesheet" />
    <title>贵农手机支付</title>
</head>
<body>
    <script src="~/Scripts/Wapjs/Wapjquery-1.10.2.min.js"></script>
    <script src="~/Scripts/layer/layer.js"></script>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.signalR-2.2.1.js"></script>

    <div class="hearder">
        <i class="bankGo" onclick="back()"></i>
        <p class="titleName">@(Model.Success ? "支付方式" : "支付出错")</p>
        <p>@(Model.Success ? Model.Result.ExpireMessage : "")</p>
    </div>
    @if (Model.Success)
    {
        <div class="payList">
            <ul>
                @if (Model.Result.IsShowProtocolPay())
                {
                    <li>
                        <div>
                            <label>
                                <input name="paytype" oType="Gyb.ProtocolPay" type="radio">
                                <img class="zhifu" src="~/Content/Wap/images/Gyb.ProtocolPay.png" />
                                <span class="payNames">快捷支付</span>
                            </label>
                        </div>
                    </li>
                }
                @if (Model.Result.IsSupportChannel("WeiXin.JSAPI"))
                {
                    if (ViewBag.IsWeixin)
                    {
                        <li id="WeiXinJSAPI">
                            <div>
                                <label>
                                    <input name="paytype" oType="WeiXin.JSAPI" type="radio">
                                    <img class="zhifu" src="~/Content/Wap/images/WeiXin.JSAPI.png" />
                                    <span class="payNames">微信</span>
                                </label>
                            </div>
                            <script>
                                $(function () {
                                    if (is_weixn()) {
                                        $("#WeiXinJSAPI").show();
                                    } else {
                                        $("#WeiXinJSAPI").hide();
                                    }
                                });
                            </script>
                        </li>
                    }
                }
                @*@if (Model.Result.IsSupportChannel("WeiXin.MWEB"))
                    {*@
                @if (!ViewBag.IsWeixin)
                {
                    <li id="WeiXinMWEB">
                        <div>
                            <label>
                                <input name="paytype" oType="WeiXin.MWEB" type="radio">
                                <img class="zhifu" src="~/Content/Wap/images/WeiXin.JSAPI.png" />
                                <span class="payNames">微信</span>
                            </label>
                        </div>
                        <script>
                            $(function () {
                                if (is_weixn()) {
                                    $("#WeiXinMWEB").hide();
                                } else {
                                    $("#WeiXinMWEB").show();
                                }
                            });
                        </script>
                    </li>
                }
                @*}*@
                @if (Model.Result.IsSupportChannel("AliPay.MoblieWap"))
                {
                    <li id="zhifubao">
                        <div>
                            <label>
                                <input name="paytype" oType="AliPay.MoblieWap" type="radio">
                                <img class="zhifu" src="~/Content/Wap/images/AliPay.MoblieWap.png" />
                                <span class="payNames">支付宝</span>
                            </label>
                        </div>
                        <script>
                            $(function () {
                                if (is_weixn()) {
                                    $("#zhifubao").hide();
                                } else {
                                    $("#zhifubao").show();
                                }
                            });
                        </script>
                    </li>
                }
            </ul>
        </div>
        <p id="orderNames">@Model.Result.Subject @Model.Result.Body</p>
        <div class="footers">
            <p class="footPrice">合计<span id="ordersMoneys">¥ @Model.Result.OrderMoney.ToString("N2")</span>元</p>
            <a class="linkBank" href="@Url.Action("BankList","Wap",new { id= @Model.Result.ApplyId })" id="submits" onclick="paysMonry()"><button class="payBtn" disabled>去支付</button></a>
        </div>
        <script>
            /**
             * 判断当前是否在微信浏览器
             */
            function is_weixn() {
                var ua = navigator.userAgent.toLowerCase();
                if (ua.match(/MicroMessenger/i) == "micromessenger") {
                        return true;
                }
                return false;
            }

            /**
             * 返回上一级
             */
            function back() {
                history.go(-1);
            };

            /**
             * 确认支付
             */
            function paysMonry() {

                var index = layer.load(1, { shade: [0.1, '#000'] });
                var that = $('input:radio[name="paytype"]:checked').eq(0);
                var getVes = that.attr('oType');
                var res = JSON.parse(localStorage.apply);
                res.ChannelId = getVes;
                res.BankCradPayId = "";
                if (getVes == "Gyb.ProtocolPay") {
                    layer.close(index);
                    $("#submits").attr("href", "@Url.Action("BankList", "Wap", new { id = @Model.Result.ApplyId })");
                    return;
                } else if (getVes == "WeiXin.JSAPI") {
                    $("#submits").attr("href", "javascript:void(0)");
                    $.post("/Wap/PayChannel", res, function (data) {
                        if (data.success) {
                            layer.close(index);
                            openWeiXin(data.result.payParams);
                        }
                        else {
                            layer.close(index);
                            layer.msg(data.error.message, {
                                time: 5000, //1s后自动关闭
                            });
                        }
                    })
                } else if (getVes == "WeiXin.MWEB") {
                    $("#submits").attr("href", "javascript:void(0)");
                    $.post("/Wap/PayChannel", res, function (data) {
                        if (data.success) {
                            layer.close(index);
                            window.location.href = data.result.payUrl;
                        }
                        else {
                            layer.close(index);
                            layer.msg(data.error.message, {
                                time: 5000, //1s后自动关闭
                            });
                        }
                    })
                }else {
                    $("#submits").attr("href", "javascript:void(0)");
                    $.post("/Wap/PayChannel", res, function (data) {
                        if (data.success) {
                            layer.close(index);
                            location.replace(data.result.payParams);
                        }
                        else {
                            layer.close(index);
                            layer.msg(data.error.message, {
                                time: 5000, //1s后自动关闭
                            });
                        }
                    })
                }
            };

            //打开微信，唤醒支付
            function openWeiXin(data) {
                WeixinJSBridge.invoke(
               'getBrandWCPayRequest', JSON.parse(data),
               function (res) {
                   if (res.err_msg == "get_brand_wcpay_request:ok") {
                       layer.msg("支付完成,正在处理...", {
                           time: 5000, //1s后自动关闭
                       });
                   }
               });
            }

            /**
             * 初始化
             */
            function init() {
                 localStorage.payMoney = " @Model.Result.OrderMoney.ToString("N2")";
                var applys = {};
                applys.ApplyId = "@Model.Result.ApplyId";
                localStorage.apply = JSON.stringify(applys);


                //支付列表切换样式
                $(".payList ul").on("click", "input[name='paytype']", function () {
                    $(".payList label").removeClass("actives");
                    $(this).parent().addClass("actives");
                    $(".payBtn").removeAttr('disabled');
                    $(".payBtn").css("background", "#ff6600");
                });
                var connection = $.hubConnection();
                var notifyProxy = connection.createHubProxy('notifyHub');
                notifyProxy.on('notify', function (msg) {
                    var obj = JSON.parse(msg);
                    location.href = "/Wap/PayRedirectResult?tradeNo=" + obj.tradeNo;
                });
                connection.start().done(function () {
                    notifyProxy.invoke('registerApply', '@Model.Result.ApplyId');
                });
            };

            $(function () {
                init();
            });
        </script>
    }
    else
    {
        <h2 class="errMassage">
            <img src="~/Content/Wap/Wapimages/error.png" style="margin:0 auto;" />
            @(Model.Error != null ? Model.Error.Message + "(errorCode:" + Model.Error.Code + ")" : "未知原因")
        </h2>
    }
</body>
</html>
